package com.tarim.streetdance.api;

import com.tarim.streetdance.model.College;
import com.tarim.streetdance.service.CollegeService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/college")
public class CollegeApi {
    @Autowired
    CollegeService collegeService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 查询全部大学
     * @param request
     * @param name
     * @return
     */
    @RequestMapping("/selectAll")
    public JsonResult selectAll(HttpServletRequest request,
                                @RequestParam(value = "name", defaultValue = "") String name){
        JsonResult js;
        try {
            College college = new College();
            college.setName(name);
            List list = collegeService.selectAll(college);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", list);
        }catch (Exception ex) {
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }

    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("/selectByCondition")
    public JsonResult selectByCondition(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "name", defaultValue = "") String name){
        JsonResult js;
        try {
            PageBean<College> pageBean = new PageBean<>(pageIndex, pageSize);
            College college = new College();
            college.setName(name);
            pageBean = collegeService.selectForPage(college, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }

    /**
     * 修改大学信息
     * @param college
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult update(College college){
        JsonResult js;
        try {
            int num = collegeService.updateByPrimaryKeySelective(college);
            if (num != 0) {
                js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
            }else {
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }
}
